package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.system.entity.Mark;
import cc.mrbird.febs.system.service.IMarkService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author MrBird
 * @date 2019-07-20 23:15:45
 */
@Slf4j
@Validated
@Controller
public class MarkController extends BaseController {

    @Resource
    private IMarkService markService;

   /* @GetMapping(FebsConstant.VIEW_PREFIX + "mark")
    private String markIndex(){
        return FebsUtil.view("mark/mark");
    }
*/
    @GetMapping("mark")
    @RequiresPermissions("mark:list")
    public FebsResponse getAllMarks(Mark mark) {
        return new FebsResponse().success().data(markService.findMarks(mark));
    }
    @Log("查询图纸的备注信息")
    @GetMapping("markAll")
    @ResponseBody
    public FebsResponse getAllMarksForSelect(Integer id) {
        return new FebsResponse().success().data(markService.findMarks(id));
    }

    @GetMapping("mark/list")
    @RequiresPermissions("mark:list")
    public FebsResponse markList(QueryRequest request, Mark mark) {
        Map<String, Object> dataTable = getDataTable(this.markService.findMarks(request, mark));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("新增Mark")
    @PostMapping("mark")
    //@RequiresPermissions("mark:add")
    @ResponseBody
    public FebsResponse addMark(@Valid Mark mark) throws FebsException {
        try {
            this.markService.createMark(mark);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增Mark失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除Mark")
    @GetMapping("mark/delete")
    @RequiresPermissions("mark:delete")
    public FebsResponse deleteMark(Mark mark) throws FebsException {
        try {
            this.markService.deleteMark(mark);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除Mark失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改Mark")
    @PostMapping("mark/update")
    @RequiresPermissions("mark:update")
    public FebsResponse updateMark(Mark mark) throws FebsException {
        try {
            this.markService.updateMark(mark);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改Mark失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("mark/excel")
    @RequiresPermissions("mark:export")
    public void export(QueryRequest queryRequest, Mark mark, HttpServletResponse response) throws FebsException {
        try {
            List<Mark> marks = this.markService.findMarks(queryRequest, mark).getRecords();
            ExcelKit.$Export(Mark.class, response).downXlsx(marks, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
